package com.nic.view.widget;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.nic.view.R;

/**
 * Created by Administrator on 2018/11/2.
 */

public class ToolBarHelper {
    private FrameLayout mRootLayout;
    private Toolbar mToolbar;
    private View mTargetView;
    private FrameLayout mTargetLayout;
    private TextView tvTitle;
    private TextView tvRight;
    private ImageView ivRight;
    private ImageView ivRight2;
    private Context context;
//    private static ToolBarHelper toolBarHelper;
//
//    public static ToolBarHelper getInstance() {
//        if (toolBarHelper == null) {
//            synchronized (ToolBarHelper.class) {
//                toolBarHelper = new ToolBarHelper();
//            }
//        }
//        return toolBarHelper;
//    }

    public ToolBarHelper() {

    }

    public void setContentView(Context context, int layoutID) {
        this.context = context;
        this.mTargetView = LayoutInflater.from(context).inflate(layoutID, null);
        initView(context);
    }

    public void setContentView(Context context, View view) {
        this.context = context;
        this.mTargetView = view;
        initView(context);
    }

    private void initView(Context context) {
        FrameLayout.LayoutParams layoutParam = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        FrameLayout.LayoutParams layoutParamMarginTop = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParamMarginTop.topMargin = context.getResources().getDimensionPixelOffset(R.dimen.dimen_44);
        mRootLayout = new FrameLayout(context);
        mRootLayout.setFitsSystemWindows(true);
        mRootLayout.setLayoutParams(layoutParam);

        View view = LayoutInflater.from(context).inflate(R.layout.toolbar_helper, mRootLayout);
        mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        mToolbar.setTitle("");
        tvTitle = (TextView) view.findViewById(R.id.toolbar_title_tv);
        tvRight = (TextView) view.findViewById(R.id.toolbar_right_tv);
        ivRight = (ImageView) view.findViewById(R.id.toolbar_right_iv);
        ivRight2 = (ImageView) view.findViewById(R.id.toolbar_right_iv2);
        mTargetLayout = new FrameLayout(context);
        mTargetLayout.setLayoutParams(layoutParamMarginTop);
        mTargetLayout.addView(mTargetView);
        mRootLayout.addView(mTargetLayout);
        mRootLayout.setBackgroundColor(ContextCompat.getColor(context, R.color.window_color));

    }

    public void setToolbarVisibility(int visible) {
        FrameLayout.LayoutParams layoutParamMarginTop = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getToolbar().setVisibility(visible);
        int height = context.getResources().getDimensionPixelOffset(R.dimen.toolbar_height);
        if (visible == View.GONE) {
            height = 0;
        }
        layoutParamMarginTop.topMargin = height;
        mTargetLayout.setLayoutParams(layoutParamMarginTop);
    }

    public int getStatusBarHeight(Context context) {
        int height = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            height = context.getResources().getDimensionPixelSize(resourceId);
        }
        return height;
    }

    public TextView getTvRight() {
        return tvRight;
    }

    public ImageView getIvRight() {
        return ivRight;
    }

    public ImageView getIvRight2() {
        return ivRight2;
    }

    public TextView getTvTitle() {
        return tvTitle;
    }

    public FrameLayout getRootLayout() {
        return mRootLayout;
    }

    public View getTargetView() {
        return mTargetView;
    }

    public Toolbar getToolbar() {
        return mToolbar;
    }

    public void setToolbarTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
        }
    }
}
